﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum PlatformGroupType
{
    Grass, Winter
}


public class PlatformSpawner : MonoBehaviour
{
    public Vector3 startSpawnPos;
    public int milestoneCount = 10;
    public float fallTime = 2f;
    public float minFallTime = 0.5f;
    public float multiple = 0.9f;

    private int spawnPlatformCount;
    private ManagerVars vars;
    private Vector3 platformSpawnPositon;
    private bool isLeftSpawn = false;
    private Sprite selectPlatformSprite;
    private PlatformGroupType groupType;
    private bool isSpikeSpawnLeft;
    private Vector3 spikerDirPlatformPos;
    private int spikerCommonPlatformCount;
    private bool isSpawnSpikerPlatform = false;

    private void Awake()
    {
        EventCenter.AddListener(EventType.DecidePath, DecidePath);
    }

    private void OnDestroy()
    {
        EventCenter.RemoveListener(EventType.DecidePath, DecidePath);
    }

    // Start is called before the first frame update
    void Start()
    {
        platformSpawnPositon = startSpawnPos;
        vars = ManagerVars.Instance;

        RandomPlatformTheme();
        spawnPlatformCount = 8;
        for (int i = 0; i < 5; i++)
        {
            DecidePath();
        }
        GameObject go = Instantiate(vars.characterPre);
        go.transform.position = new Vector3(0, -1.0f, 0);

    }

    private void Update()
    {
        if (GameManager.Instance.IsGameStarted && !GameManager.Instance.IsGameOver && !GameManager.Instance.IsPause)
        {
            updateFallTime();
        }
    }

    private void updateFallTime()
    {
        if (GameManager.Instance.GameScore > milestoneCount)
        {
            milestoneCount *= 2;
            fallTime *= multiple;
            if (fallTime < minFallTime)
            {
                fallTime = minFallTime;
            }
        }
        
    }

    private void DecidePath()
    {
        if (isSpawnSpikerPlatform)
        {
            SpawnSpikerCommonPlatform();
            return;
        }
        if (spawnPlatformCount >= 0 )
        {
            spawnPlatformCount--;
            SpawnPlatform();
        }
        else
        {
            isLeftSpawn = !isLeftSpawn;
            spawnPlatformCount = Random.Range(1, 4);
            SpawnPlatform();
        }

    }

    private void RandomPlatformTheme()
    {
        int ran = Random.Range(0, vars.PlatformThemeList.Count);
        selectPlatformSprite = vars.PlatformThemeList[ran];

        if (ran == 2)
        {
            groupType = PlatformGroupType.Winter;
        }
        else
        {
            groupType = PlatformGroupType.Grass;
        }
    }

    private void SpawnPlatform()
    {
        if (spawnPlatformCount >= 1)
        {
            SpawnNormalPlatform();
        }
        else
        {
            int ran = Random.Range(0, 3);
            if (ran == 0)
            {
                SpawnCommonPlatformGroup();
            }
            else if (ran == 1)
            {
                if (groupType == PlatformGroupType.Grass)
                {
                    SpawnGrassPlatformGroup();
                }
                else
                {
                    SpawnWinterPlatformGroup();
                }
            }
            else
            {
                SpawnSpikerPlatform();
                spikerCommonPlatformCount = Random.Range(2, 5);
                isSpawnSpikerPlatform = true;
                if (isSpikeSpawnLeft)
                {
                    spikerDirPlatformPos = new Vector3(platformSpawnPositon.x - 1.65f, platformSpawnPositon.y + vars.nextYPos, 0.0f);
                }
                else
                {
                    spikerDirPlatformPos = new Vector3(platformSpawnPositon.x + 1.65f, platformSpawnPositon.y + vars.nextYPos, 0.0f);
                }

            }
        }

        if (0 == Random.Range(0, 8) && GameManager.Instance.IsPlayerMove)
        {
            GameObject go = ObjectPool.Instance.GetDiamond();
            go.transform.position = new Vector3(platformSpawnPositon.x, platformSpawnPositon.y + 0.5f, 0.0f);
            go.SetActive(true);
        }

        if (isLeftSpawn)
        {
            platformSpawnPositon = new Vector3(platformSpawnPositon.x - vars.nextXPos, platformSpawnPositon.y + vars.nextYPos, 0);
        }
        else
        {
            platformSpawnPositon = new Vector3(platformSpawnPositon.x + vars.nextXPos, platformSpawnPositon.y + vars.nextYPos, 0);
        }
    }

    private void SpawnNormalPlatform()
    {
        GameObject go = ObjectPool.Instance.GetNormalPlatform();
        go.SetActive(true);
        go.transform.position = platformSpawnPositon;
        go.GetComponent<PlatformScript>().Init(selectPlatformSprite, fallTime);
    }

    private void SpawnCommonPlatformGroup()
    {
        GameObject go = ObjectPool.Instance.GetCommonPlatform();
        go.SetActive(true);
        go.transform.position = platformSpawnPositon;
        go.GetComponent<PlatformScript>().Init(selectPlatformSprite, fallTime);
    }

    private void SpawnGrassPlatformGroup()
    {
        GameObject go = ObjectPool.Instance.GetGrassPlatform();
        go.SetActive(true);
        go.transform.position = platformSpawnPositon;
        go.GetComponent<PlatformScript>().Init(selectPlatformSprite, fallTime);
    }

    private void SpawnWinterPlatformGroup()
    {
        GameObject go = ObjectPool.Instance.GetWinterPlatform();
        go.SetActive(true);
        go.transform.position = platformSpawnPositon;
        go.GetComponent<PlatformScript>().Init(selectPlatformSprite, fallTime);
    }

    private void SpawnSpikerPlatform()
    {
        GameObject go;
        if (isLeftSpawn)
        {
            isSpikeSpawnLeft = false;
            go = ObjectPool.Instance.GetSpikerPlatformRight();
        }
        else
        {
            isSpikeSpawnLeft = true;
            go = ObjectPool.Instance.GetSpikerPlatformLeft();
        }
        go.SetActive(true);
        go.transform.position = platformSpawnPositon;
        go.GetComponent<PlatformScript>().Init(selectPlatformSprite, fallTime);
    }

    private void SpawnSpikerCommonPlatform()
    {
        if (spikerCommonPlatformCount > 0)
        {
            spikerCommonPlatformCount--;


            GameObject go = ObjectPool.Instance.GetNormalPlatform();
            go.SetActive(true);
            go.transform.position = platformSpawnPositon;
            go.GetComponent<PlatformScript>().Init(selectPlatformSprite, fallTime);

            go = ObjectPool.Instance.GetNormalPlatform();
            go.SetActive(true);
            go.transform.position = spikerDirPlatformPos;
            go.GetComponent<PlatformScript>().Init(selectPlatformSprite, fallTime);
            if (isSpikeSpawnLeft)
            {
                platformSpawnPositon = new Vector3(platformSpawnPositon.x + vars.nextXPos, platformSpawnPositon.y + vars.nextYPos, 0);
                spikerDirPlatformPos = new Vector3(spikerDirPlatformPos.x - vars.nextXPos, spikerDirPlatformPos.y + vars.nextYPos, 0);
            }
            else
            {
                platformSpawnPositon = new Vector3(platformSpawnPositon.x - vars.nextXPos, platformSpawnPositon.y + vars.nextYPos, 0);
                spikerDirPlatformPos = new Vector3(spikerDirPlatformPos.x + vars.nextXPos, spikerDirPlatformPos.y + vars.nextYPos, 0);
            }
        }
        else
        {
            isSpawnSpikerPlatform = false;
            DecidePath();
        }
    }
}
